# Maintainer: @naveen521kk on Github, Naveen M K <naveen521kk@gmail.com>

_realname=texlive-core
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2022.20220501
_revnr=${pkgver#2022.}
pkgrel=2
pkgdesc="TeX Live core distribution (mingw-w64)"
license=('GPL')
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
depends=(
    "${MINGW_PACKAGE_PREFIX}-texlive-bin"
    "${MINGW_PACKAGE_PREFIX}-perl"
)
makedepends=("${MINGW_PACKAGE_PREFIX}-cc")
optdepends=(
    #'dialog:      for texconfig'
    "${MINGW_PACKAGE_PREFIX}-ghostscript: for epstopdf, epspdf and other ConTeXt tools"
    #'java-runtime: for utilities like arara, texplate'
    #'perl-tk:     for texdoctk'
    #'psutils:     to manipulate the output of dvips'
    "${MINGW_PACKAGE_PREFIX}-python:      for de-macro, dviasm, pythontex"
    "${MINGW_PACKAGE_PREFIX}-ruby:        for old ConTeXT MkII and epspdf"
    #"t1utils:     can be useful when installing Type1 fonts"
)
groups=(
    "${MINGW_PACKAGE_PREFIX}-texlive-full"
    "${MINGW_PACKAGE_PREFIX}-texlive-scheme-basic"
    "${MINGW_PACKAGE_PREFIX}-texlive-scheme-full"
    "${MINGW_PACKAGE_PREFIX}-texlive-scheme-gust"
    "${MINGW_PACKAGE_PREFIX}-texlive-scheme-medium"
    "${MINGW_PACKAGE_PREFIX}-texlive-scheme-small"
    "${MINGW_PACKAGE_PREFIX}-texlive-scheme-tetex"
    "${MINGW_PACKAGE_PREFIX}-texlive-scheme-context"
)
conflicts=(
    "${MINGW_PACKAGE_PREFIX}-texlive-xetex"
    "${MINGW_PACKAGE_PREFIX}-texlive-context"
    "${MINGW_PACKAGE_PREFIX}-texlive-base"
    "${MINGW_PACKAGE_PREFIX}-texlive-latex-base"
)
provides=(
    "${MINGW_PACKAGE_PREFIX}-texlive-xetex"
    "${MINGW_PACKAGE_PREFIX}-texlive-context"
    "${MINGW_PACKAGE_PREFIX}-texlive-base"
    "${MINGW_PACKAGE_PREFIX}-texlive-latex-base"
)
replaces=(
    "${MINGW_PACKAGE_PREFIX}-texlive-xetex"
    "${MINGW_PACKAGE_PREFIX}-texlive-context"
    "${MINGW_PACKAGE_PREFIX}-texlive-base"
    "${MINGW_PACKAGE_PREFIX}-texlive-latex-base"
)
url='https://tug.org/texlive/'
source=("${_realname}-${_revnr}.tar.xz::https://github.com/msys2/msys2-texlive/releases/download/${_revnr}/texlive-core-${_revnr}.tar.xz"
    "${_realname}-extra-files.tar.xz::https://github.com/msys2/msys2-texlive/releases/download/${_revnr}/${_realname}-extra-files.tar.xz"
    "mktexlsr.hook.in"
    "mktexlsr.script.in"
    "texlive-updmap.hook.in"
    "texlive-updmap.script.in"
    "texlive-fmtutil.hook.in"
    "texlive-fmtutil.script.in"
    "texmf.cnf.in"
    "texmfcnf.lua.in"
    "09-texlive-fonts.conf.in")
install=${_realname}-${MSYSTEM}.install
backup=("${MINGW_PREFIX:1}/etc/texmf/web2c/texmf.cnf"
    "${MINGW_PREFIX:1}/etc/texmf/dvipdfmx/dvipdfmx.cfg"
    "${MINGW_PREFIX:1}/etc/texmf/dvips/config/config.ps"
    "${MINGW_PREFIX:1}/etc/texmf/tex/generic/config/language.dat"
    "${MINGW_PREFIX:1}/etc/texmf/tex/generic/config/language.def"
    "${MINGW_PREFIX:1}/etc/texmf/tex/generic/tex-ini-files/pdftexconfig.tex"
    #"${MINGW_PREFIX:1}/etc/texmf/ttf2pk/ttf2pk.cfg"
    "${MINGW_PREFIX:1}/etc/texmf/web2c/fmtutil.cnf"
    "${MINGW_PREFIX:1}/etc/texmf/web2c/mktex.cnf"
    "${MINGW_PREFIX:1}/etc/texmf/xdvi/XDvi")
sha256sums=(
    "43d3fd8c1e58c30a8af13174c28e78726e1ed7b59862890bd05cfb8ad07097e7"
    "c3dd7734f0818d7140dde3a7b3b6f3b96d5d073ac54724c5aa97ae1640e0ed1b"
    '542f68cd8e1a00f1598db8532e70e1bb77adc0516daee84e5aa92707f58fdf04'
    '773e9c8f60c4241a57adafda8abac765c53f7e6e24c86b87366b037ade29077b'
    'bffaa8a7f5d78aed138a760dfa1073ad7c419a61a9739cafbc28dce330aae911'
    'e154d58543dc8d9957f502b3d162023f0be4a650860d3be5289ff2ac880ccca2'
    'd615d040d1943bcdf7b1c7a649644a7bd47703317b79963062a058e1300602f3'
    'dd286733d0b596708ee049aa22d641129d20f61113298c6f11ad37500bf9f1fb'
    '3bc32f119b672015d75d136a3a506efefac6f7b8e01a9a6c95c10c4336931b26'
    '8ededf35132ffbe26434c0c778b3d4a159ce428452abe6ebc8d94161f02bd7e9'
    '87eddde958848d041ded1625e14009e89b3bd5953e7ae39b6aac2f65d3856485'
)

build() {
    cd "$srcdir"
    msg "   --> extracting all packages... "
    for p in *.tar.xz; do
        bsdtar -xf $p
        echo $p
    done
    echo "done"
    rm -rf source doc

    cd "$srcdir/texmf-dist/scripts/context/stubs/source"
    cc -Os -s -shared -Wl,--out-implib=libmtxrun.dll.a -o mtxrun.dll mtxrun_dll.c
    cc -Os -s -o mtxrun.exe mtxrun_exe.c -L./ -lmtxrun
    cd ..

}

copy_file_message() {
    local from="$1"
    local to="$2"
    msg2 "Copying $from to $to"
    cp $from $to
}

package() {
    cd "$srcdir"

    msg "Installing Pacman hooks"
    # First lets sed all files for prefix changes.
    for hook in 'mktexlsr' 'texlive-fmtutil' 'texlive-updmap'; do
        local hook_path="${srcdir}/${MINGW_PACKAGE_PREFIX}-${hook}.hook"
        cp "${srcdir}/${hook}.hook.in" "${hook_path}"
        sed -s "s|@MINGW_HOOK_TARGET_PREFIX@|${MINGW_PREFIX:1}|g" -i "${hook_path}"
        sed -s "s|@MINGW_PREFIX@|${MINGW_PREFIX}|g" -i "${hook_path}"
        sed -s "s|@MINGW_PACKAGE_PREFIX@|${MINGW_PACKAGE_PREFIX}|g" -i "${hook_path}"
        sed -s "s|@TEMP@|$(cygpath -w $TMP)|g" -i "${hook_path}"
        install -Dt "$pkgdir/usr/share/libalpm/hooks" -m644 "${hook_path}"
    done

    for script in 'mktexlsr' 'texlive-fmtutil' 'texlive-updmap'; do
        local script_path="${srcdir}/${MINGW_PACKAGE_PREFIX}-${script}.script"
        cp "${srcdir}/${script}.script.in" "${script_path}"
        sed -s "s|@MINGW_PREFIX@|${MINGW_PREFIX}|g" -i "${script_path}"
        sed -s "s|@MINGW_HOOK_TARGET_PREFIX@|${MINGW_PREFIX:1}|g" -i "${script_path}"
        install -Dt "$pkgdir/usr/share/libalpm/scripts" -m644 "${script_path}"
    done

    for file_conf in '09-texlive-fonts.conf' 'texmfcnf.lua' 'texmf.cnf'; do
        local conf_path="${srcdir}/${file_conf}"
        cp "${srcdir}/${file_conf}.in" "${conf_path}"
        sed -s "s|@MINGW_HOOK_TARGET_PREFIX@|${MINGW_PREFIX:1}|g" -i "${conf_path}"
        sed -s "s|@MINGW_PREFIX@|${MINGW_PREFIX}|g" -i "${conf_path}"
        sed -s "s|@MINGW_PACKAGE_PREFIX@|${MINGW_PACKAGE_PREFIX}|g" -i "${conf_path}"
        sed -s "s|@TEMP@|$(cygpath -u $TMP)|g" -i "${conf_path}"
    done

    msg "Installing Package"
    # Install packages.
    install -m755 -d "${pkgdir}${MINGW_PREFIX}/var/lib/texmf/msys2/installedpkgs"
    sed -i '/^#/d' CONTENTS
    install -m644 CONTENTS "${pkgdir}${MINGW_PREFIX}/var/lib/texmf/msys2/installedpkgs/${pkgname}_${_revnr}.pkgs"
    install -m644 $_realname.maps "${pkgdir}${MINGW_PREFIX}/var/lib/texmf/msys2/installedpkgs/"
    install -m644 $_realname.fmts "${pkgdir}${MINGW_PREFIX}/var/lib/texmf/msys2/installedpkgs/"

    # language files for hooks
    sed -i 's/\% test//' $_realname.dat
    install -m644 $_realname.dat "${pkgdir}${MINGW_PREFIX}/var/lib/texmf/msys2/installedpkgs/"

    sed -i 's/-- test//' $_realname.dat.lua
    install -m644 $_realname.dat.lua "${pkgdir}${MINGW_PREFIX}/var/lib/texmf/msys2/installedpkgs/"

    sed -i 's/\% test//' $_realname.def
    install -m644 $_realname.def "${pkgdir}${MINGW_PREFIX}/var/lib/texmf/msys2/installedpkgs/"

    install -m755 -d "${pkgdir}${MINGW_PREFIX}/share"
    wanteddirs=$(for d in *; do test -d $d && [[ $d != texmf* ]] && echo $d; done) || true
    for dir in $wanteddirs; do
        find $dir -type d -exec install -d -m755 "${pkgdir}${MINGW_PREFIX}"/share/texmf-dist/'{}' \;
        find $dir -type f -exec install -m644 '{}' "${pkgdir}${MINGW_PREFIX}"/share/texmf-dist/'{}' \;
    done
    find texmf-dist -type d -exec install -d -m755 "${pkgdir}${MINGW_PREFIX}"/share/'{}' \;
    find texmf-dist -type f -exec install -m644 '{}' "${pkgdir}${MINGW_PREFIX}"/share/'{}' \;
    find texmf-dist -type f -executable -exec chmod 755 "${pkgdir}${MINGW_PREFIX}"/share/'{}' \;

    #############################################################
    ### install texmf tree
    msg "Installing the /etc/texmf tree"
    install -d -m755 "${pkgdir}${MINGW_PREFIX}/etc/texmf/web2c/"
    install -d -m755 "${pkgdir}${MINGW_PREFIX}/etc/texmf/chktex/"
    install -d -m755 "${pkgdir}${MINGW_PREFIX}/etc/texmf/dvips/config/"
    install -d -m755 "${pkgdir}${MINGW_PREFIX}/etc/texmf/dvipdfmx/"
    install -d -m755 "${pkgdir}${MINGW_PREFIX}/etc/texmf/tex/generic/config/"
    install -d -m755 "${pkgdir}${MINGW_PREFIX}/etc/texmf/tex/generic/tex-ini-files/"
    install -d -m755 "${pkgdir}${MINGW_PREFIX}/etc/texmf/ttf2pk/"
    install -d -m755 "${pkgdir}${MINGW_PREFIX}/etc/texmf/xdvi/"

    install -d -m755 "${pkgdir}${MINGW_PREFIX}/share/fontconfig/conf.avail"
    install -m644 "$srcdir"/09-texlive-fonts.conf \
        "${pkgdir}${MINGW_PREFIX}/share/fontconfig/conf.avail/"

    # Remove manpages (already in texlive-bin).
    rm -rf "${pkgdir}${MINGW_PREFIX}/share/texmf-dist/doc/man"

    msg "Copying config files to TEXMFCONFIG tree"
    # copy config files to $TEXMFCONFIG tree
    copy_file_message "${pkgdir}${MINGW_PREFIX}/share/texmf-dist/web2c/mktex.cnf" \
        "${pkgdir}${MINGW_PREFIX}/etc/texmf/web2c/"

    copy_file_message "${pkgdir}${MINGW_PREFIX}/share/texmf-dist/web2c/updmap-hdr.cfg" \
        "${pkgdir}${MINGW_PREFIX}/etc/texmf/web2c/"

    copy_file_message "${pkgdir}${MINGW_PREFIX}/share/texmf-dist/web2c/fmtutil-hdr.cnf" \
        "${pkgdir}${MINGW_PREFIX}/etc/texmf/web2c/fmtutil.cnf"

    copy_file_message "${pkgdir}${MINGW_PREFIX}/share/texmf-dist/dvips/config/config.ps" \
        "${pkgdir}${MINGW_PREFIX}/etc/texmf/dvips/config/"

    copy_file_message "${pkgdir}${MINGW_PREFIX}/share/texmf-dist/dvipdfmx/dvipdfmx.cfg" \
        "${pkgdir}${MINGW_PREFIX}/etc/texmf/dvipdfmx/"

    copy_file_message "${pkgdir}${MINGW_PREFIX}/share/texmf-dist/tex/generic/tex-ini-files/pdftexconfig.tex" \
        "${pkgdir}${MINGW_PREFIX}/etc/texmf/tex/generic/tex-ini-files/"

    copy_file_message "${pkgdir}${MINGW_PREFIX}/share/texmf-dist/tex/generic/config/language.us" \
        "${pkgdir}${MINGW_PREFIX}/etc/texmf/tex/generic/config/"

    copy_file_message "${pkgdir}${MINGW_PREFIX}/share/texmf-dist/tex/generic/config/language.us.def" \
        "${pkgdir}${MINGW_PREFIX}/etc/texmf/tex/generic/config/"

    copy_file_message "${pkgdir}${MINGW_PREFIX}/share/texmf-dist/tex/generic/config/language.us.lua" \
        "${pkgdir}${MINGW_PREFIX}/etc/texmf/tex/generic/config/"

    # collection fontutils
    # copy_file_message "${pkgdir}${MINGW_PREFIX}/share/texmf-dist/ttf2pk/ttf2pk.cfg" \
    # 	  "${pkgdir}${MINGW_PREFIX}/etc/texmf/ttf2pk/"

    copy_file_message "${pkgdir}${MINGW_PREFIX}/share/texmf-dist/xdvi/XDvi" \
        "${pkgdir}${MINGW_PREFIX}/etc/texmf/xdvi/"

    msg 'remove TL specific warnings in the language.{dat,def} files'
    # remove TL specific warnings in the language.{dat,def} files:
    sed -i -e '/DO NOT EDIT/,+3 d' "${pkgdir}${MINGW_PREFIX}"/etc/texmf/tex/generic/config/language.*

    msg "Replace upstream texmf.cnf with ours"
    # replace upstream texmf.cnf with ours
    rm -f "${pkgdir}${MINGW_PREFIX}"/share/texmf-dist/web2c/texmf.cnf
    install -m644 "$srcdir"/texmf.cnf "${pkgdir}${MINGW_PREFIX}"/etc/texmf/web2c/texmf.cnf
    install -m644 "${pkgdir}${MINGW_PREFIX}"/etc/texmf/web2c/texmf.cnf "${pkgdir}${MINGW_PREFIX}"/share/texmf-dist/web2c/texmf.cnf
    # replace upstream texmfcnf.lua with ours
    install -m644 "$srcdir"/texmfcnf.lua "${pkgdir}${MINGW_PREFIX}"/share/texmf-dist/web2c/texmfcnf.lua

    msg "Configure Languages Hypens"
    # remove upstream's language.*
    rm -f "${pkgdir}${MINGW_PREFIX}"/share/texmf-dist/tex/generic/config/language.dat
    rm -f "${pkgdir}${MINGW_PREFIX}"/share/texmf-dist/tex/generic/config/language.def
    rm -f "${pkgdir}${MINGW_PREFIX}"/share/texmf-dist/tex/generic/config/language.dat.lua

    msg "Install Perl libraries"
    # install Perl libraries
    mv "${pkgdir}${MINGW_PREFIX}"/share/texmf-dist/tlpkg "${pkgdir}${MINGW_PREFIX}"/share
    rm -rf "${pkgdir}${MINGW_PREFIX}"/share/tlpkg/tlpobj

    msg "remove Upstream updmap.cfg and fmtutil.cnf"
    # remove upstream updmap.cfg: it contains too many maps.
    rm "${pkgdir}${MINGW_PREFIX}/share/texmf-dist/web2c/updmap.cfg"
    # remove upstream fmtutil.cnf: it will be autogenerated
    rm "${pkgdir}${MINGW_PREFIX}/share/texmf-dist/web2c/fmtutil.cnf"

    # Create lauchers for all these Scripts.
    # There is a `runscript.exe` which can be copied
    # with these script names and things should work
    # automatically. It is from texlive-bin
    msg "Creating Launchers."
    # get the list from
    # this should be in source.
    # remove comment
    sed -i 's/# This file contains linked scripts list for the package.//' "${srcdir}/${_realname}.scripts"
    # remove useless files
    sed -i /.*NEWS/d "${srcdir}/${_realname}.scripts"

    . "${srcdir}/${_realname}.scripts"
    install -m755 -d "${pkgdir}${MINGW_PREFIX}"/bin
    for _script in ${linked_scripts}; do
        _scriptbase=$(basename $_script)
        _scriptbase=${_scriptbase%.*}
        install -D -m755 ${MINGW_PREFIX}/bin/runscript.exe "${pkgdir}${MINGW_PREFIX}/bin/${_scriptbase}.exe"
    done

    # now copy mktexlsr
    install -D -m755 "${pkgdir}${MINGW_PREFIX}/share/texmf-dist/scripts/texlive/mktexlsr.pl" \
        "${pkgdir}${MINGW_PREFIX}/bin/mktexlsr"

    # install context stuff seperately
    for _script in context contextjit luatools mtxrun mtxrunjit texexec texmfstart
    do
        install -D -m755 ${srcdir}/texmf-dist/scripts/context/stubs/source/mtxrun.exe \
            "${pkgdir}${MINGW_PREFIX}/bin/${_script}.exe"
    done
}
